package com.yellowpage.wf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.wf.dao.WorkflowNodeDao;
import com.yellowpage.wf.data.WorkflowDataUtils;
import com.yellowpage.wf.data.WorkflowNode;

public class WorkflowNodeServiceImpl extends BaseServiceImpl implements
		WorkflowNodeService {
	private WorkflowNodeDao workflowNodeDao;

	public void setWorkflowNodeDao(WorkflowNodeDao workflowNodeDao) {
		this.workflowNodeDao = workflowNodeDao;
	}

	/* White generate Method */
	public WorkflowNode getWorkflowNode(Long id) {
		return workflowNodeDao.get(id);
	}

	public List<WorkflowNode> getAllWorkflowNode() {
		return workflowNodeDao.getAll();
	}

	public List<WorkflowNode> getAllWorkFlowNodeNotStartEnd() {
		String sql = "from WorkflowNode w where w.type!='"
				+ WorkflowDataUtils.NODE_TYPE_START + "' " + " and w.type!='"
				+ WorkflowDataUtils.NODE_TYPE_END + "' ";
		return workflowNodeDao.getObjectListByConditions(sql, new Object[] {});
	}

	public List<Map> queryWorkflowNodeByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT a.WORKFLOW_NODE_ID , a.WORKFLOW_ID , a.NODE_STAUS , a.CREATE_TIME , a.LAST_UPDATE_TIME , a.NAME , a.TYPE , a.DISPLAY_NAME , "
								+ " (select distinct decode(e.name, null, '系统管理员', e.name) "
								+ " from employee e, WF_WORKFLOW_NODE wwd "
								+ " where wwd.create_user = e.employee_id(+) "
								+ " and wwd.workflow_node_id = a.workflow_node_id) as CREATE_USER,   "
								+ " (select distinct decode(e.name, null, '系统管理员', e.name) "
								+ " from employee e, WF_WORKFLOW_NODE wwd "
								+ " where wwd.last_update_user = e.employee_id(+) "
								+ " and wwd.workflow_node_id = a.workflow_node_id) as LAST_UPDATE_USER "
								+ " FROM WF_WORKFLOW_NODE a WHERE 1=1 "
								+ " and a.type not in('"
								+ WorkflowDataUtils.NODE_TYPE_START
								+ "','"
								+ WorkflowDataUtils.NODE_TYPE_END + "')")
				.addCondition(" and WORKFLOW_NODE_ID = :workflowNodeId ")
				.addCondition(" and WORKFLOW_ID = :workflowId ")
				.addCondition(" and NODE_STAUS like :nodeStaus ")
				.addCondition(" and CREATE_USER = :createUser ")
				.addCondition(
						" and CREATE_TIME = to_date(:createTime ,'yyyy-MM-dd') ")
				.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
				.addCondition(
						" and LAST_UPDATE_TIME = to_date(:lastUpdateTime ,'yyyy-MM-dd') ")
				.addCondition(" and NAME like :name ").addCondition(
						" and TYPE like :type ").addCondition(
						" and DISPLAY_NAME like :displayName ").addCondition(
						" and OWNER like :owner ");
		sqlAssistant.setTail(" ORDER BY a.NAME ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				queryMap);

		return resultList;
	}

	public Map findNode(Long workflowNodeId) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("SELECT a.WORKFLOW_NODE_ID ,a.NAME, a.DISPLAY_NAME ,  FROM WF_WORKFLOW_NODE a ,wf_workflow_path b WHERE 1=1 "
						+ " and ( b.NODE_FROM = "
						+ workflowNodeId.toString()
						+ " or b.NODE_TO = "
						+ workflowNodeId.toString()
						+ " ) "
						+ " and a.WORKFLOW_NODE_ID = "
						+ workflowNodeId.toString() + " ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		if (resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		} else {
			return null;
		}
	}
	

	public void saveWorkflowNode(WorkflowNode workflowNode) {
		workflowNode.setNodeStaus(WorkflowDataUtils.ENTITY_STATUS_ON);
		workflowNodeDao.save(workflowNode);
	}

	public void updateWorkflowNode(WorkflowNode workflowNode) {
		workflowNodeDao.update(workflowNode);
	}

	public void delete(List deleteList) {
		workflowNodeDao.remove(deleteList);
	}

	public List<WorkflowNode> getAllWorkflowNodeByWorkflowId(String workflowId) {
		return workflowNodeDao.getAllWorkflowNodeByWorkflowId(workflowId);
	}

}